package ace.states;

import ace.Main;
import ace.component.ButtonPush;
import ace.component.ButtonWheel;
import ace.game.entity.player.Profile;
import ace.system.BitmapFont;
import ace.system.Model;
import ace.system.StateManager;
import ace.system.Utilities;
import ace.system.sound.SFXPlayer;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author TimotiusNC
 */
public class StateCreateProfile extends GameState implements CommandListener {
	public StateCreateProfile(Vector param){
		super(GameState.CREATE_PROFILE, StateManager.instance().getBackgroundImage());

		m_ProfileIdx = Integer.parseInt(param.elementAt(0).toString());
		initComp();
	}

	private void initComp(){
		m_OffsetX = 100;
		m_OffsetY = 100;
		m_OKCommand	= new Command("OK", Command.OK, 1);

		m_TextBox = new TextBox("Insert your name", "", 10, TextField.INITIAL_CAPS_SENTENCE);
		m_TextBox.addCommand(m_OKCommand);
		m_TextBox.setCommandListener(this);

		m_BtnWheel = new ButtonWheel(3, 0, "/selection1.png", null);
		m_BtnWheel.addButton(1, new ButtonPush("Name", 1, m_OffsetX+20, m_OffsetY+10));
		m_BtnWheel.addButton(2, new ButtonPush("OK", 2, m_OffsetX+100, m_OffsetY+75));
		m_BtnWheel.setSelection1Offset(0, 3);
		m_BtnWheel.setImmediate(false);

		m_PlayerName = "NULL";
		m_InsertYourName = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Insert your name", 0x00000000);
		
		try {
			m_TitleImage = Image.createImage(Utilities.INTERFACE_FOLDER + "/title.png");
			m_LiteImage		= Image.createImage(Utilities.INTERFACE_FOLDER + "/lite.png");
		} catch (IOException ex) {}
	}

	public void keyPressed(int key) {
		super.keyPressed(key);

		//Is right button pressed?
		if (key == -7 || key == 35) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CANCEL);
			m_Active = false;
		}
	}

	public void update(long time, int keys) {
		super.update(time, keys);

		//Update wheel
		m_BtnWheel.update(keys);
		m_BtnWheel.forceDraw();

		if( ((keys & GameCanvas.FIRE_PRESSED) != 0) ){
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_SELECT);
			switch(m_BtnWheel.getSelectedButton().getID()){
				case 1: //TextBox clicked
					Display.getDisplay(Main.Midlet).setCurrent(m_TextBox);
					break;
				case 2: //OK clicked, save to RMS, then back
					Profile newProfile = new Profile(m_PlayerName, m_ProfileIdx);
					Model.writeModel(m_ProfileIdx, newProfile);
					m_Active = false;
					break;
			}
		}

		//Check key
		if ((keys & GameCanvas.UP_PRESSED) != 0 || (keys & GameCanvas.DOWN_PRESSED) != 0 || (keys & GameCanvas.LEFT_PRESSED) != 0 || (keys & GameCanvas.RIGHT_PRESSED) != 0) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);
		}
	}

	public void draw(Graphics g) {
		super.draw(g);

		//Dark gradient
		g.drawImage(StateManager.instance().getDarkImage(), 0, 0, Graphics.LEFT | Graphics.TOP);

		//Draw stuff
		g.drawImage(m_TitleImage, 60, 3, Graphics.LEFT | Graphics.TOP);
		g.drawImage(m_LiteImage, StateManager.instance().getWidth() - 55, 45, Graphics.LEFT | Graphics.TOP);
		m_BtnWheel.draw(g);

		if(m_PlayerName.equals("NULL"))
			g.drawImage(m_InsertYourName, m_OffsetX+100, m_OffsetY+10, Graphics.LEFT | Graphics.TOP);
		else
			g.drawImage(BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(m_PlayerName, 0x00000000), m_OffsetX+100, m_OffsetY+10, Graphics.LEFT | Graphics.TOP);
	}

	//Data
	protected ButtonWheel	m_BtnWheel;
	protected String		m_PlayerName;
	protected TextBox		m_TextBox;
	protected Command		m_OKCommand;
	protected int			m_ProfileIdx;

	//Images
	protected Image	m_InsertYourName;
	protected Image m_LiteImage;
	protected Image	m_TitleImage;
	protected int	m_OffsetX, m_OffsetY;

	public void commandAction(Command c, Displayable d) {
		if(c == m_OKCommand){
			Display.getDisplay(Main.Midlet).setCurrent(StateManager.instance());

			if(m_TextBox.getString().equals(""))
				m_PlayerName = "NULL";
			else
				m_PlayerName = m_TextBox.getString();
		}
	}
}
